package com.lzb.newloan;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lzb.newloan.utils.BaseActivity;

import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

public class LoanDetailActivity extends BaseActivity implements OnClickListener {

	private String userId;
	private TextView name,name2;
	private TextView sfz;
	private TextView money;
	private TextView day;
	private TextView zmxy;
	private TextView fk_money;
	private com.lzb.newloan.utils.CircularImage img_icon;
	private String jkid,spdz;
	private int flag;
	private TextView rzmobile;
	private TextView checknetdai;
	private TextView checkcourt;
	private TextView checksfz;
	private TextView financial_call_record;
	private TextView call_record;
	private TextView electricity_consum;
	private TextView credit_cards;
	private TextView credit_cards_fee;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loan_details);
		initView();
		initData();
	}
	
	private void initView() {
		SharedPreferences sp = getSharedPreferences("config", 0x0000);
		userId = sp.getString("userid", "");
		Intent intent = getIntent();
		jkid = intent.getStringExtra("jkid");
		spdz = intent.getStringExtra("spdz");
		flag = intent.getIntExtra("type", 0);
		RelativeLayout backpress = (RelativeLayout) findViewById(R.id.backpress);
		backpress.setVisibility(View.VISIBLE);
		backpress.setOnClickListener(this);
		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("借款人详情");	
		findViewById(R.id.fx).setOnClickListener(this);
		findViewById(R.id.look_protocal).setOnClickListener(this);
		TextView look_video = (TextView) findViewById(R.id.look_video);
		look_video.setOnClickListener(this);
		if (flag==3) {
			look_video.setVisibility(View.INVISIBLE);
		}
		name = (TextView) findViewById(R.id.name);
		name2 = (TextView) findViewById(R.id.name2);
		sfz = (TextView) findViewById(R.id.sfz);
		money = (TextView) findViewById(R.id.money);
		day = (TextView) findViewById(R.id.day);
		fk_money = (TextView) findViewById(R.id.fk_money);
		rzmobile = (TextView) findViewById(R.id.rzmobile);
		checknetdai = (TextView) findViewById(R.id.checknetdai);
		checkcourt = (TextView) findViewById(R.id.checkcourt);
		checksfz = (TextView) findViewById(R.id.checksfz);
		financial_call_record = (TextView) findViewById(R.id.financial_call_record);
		call_record = (TextView) findViewById(R.id.call_record);
		electricity_consum = (TextView) findViewById(R.id.electricity_consum);
		credit_cards = (TextView) findViewById(R.id.credit_cards);
		credit_cards_fee = (TextView) findViewById(R.id.credit_cards_fee);
		zmxy = (TextView) findViewById(R.id.zmxy);
		img_icon = (com.lzb.newloan.utils.CircularImage) findViewById(R.id.img_icon);
	}

	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case com.lzb.newloan.utils.Config.CODE_BORROWPERSONDETAIL:
					String reString = msg.obj.toString();
					try {
						JSONObject response = new JSONObject(reString);
						int error = response.getInt("error");
						System.out.println(response.toString());
						if (error != 0) {
							finish();
						} else {
							JSONObject jkinfo = response
									.getJSONObject("jkinfo");
							String realname = jkinfo.getString("name");
							name.setText(realname);
							String idcard = jkinfo.getString("idno");
							sfz.setText(idcard);
							name2.setText(realname);
							money.setText(com.lzb.newloan.utils.Formatdou.formatdou(jkinfo.getDouble("jk_money")));
							int jkqx = jkinfo.getInt("jk_date");
							if (jkqx == 1) {
								day.setText("15天");
							} else {
								day.setText("30天");
							}

							fk_money.setText(com.lzb.newloan.utils.Formatdou.formatdou(jkinfo.getDouble("jk_rate"))+"元");
							String zm_score = jkinfo.getString("zmscore");
							if (zm_score.equals("null")) {
								zmxy.setText("暂无");
							}else {
								zmxy.setText(zm_score);
							}
							rzmobile.setText(jkinfo.getString("phone"));
							checknetdai.setText(jkinfo.getString("wdblack"));
							checkcourt.setText(jkinfo.getString("fyblack"));
							checksfz.setText(jkinfo.getString("sfzblack"));
							financial_call_record.setText(jkinfo.getString("jrphone"));
							call_record.setText(jkinfo.getString("tonghua"));
							electricity_consum.setText(jkinfo.getString("jd"));
							ImageLoader.getInstance().displayImage(response.getString("living_photo"),img_icon);
						}
					} catch (JSONException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					break;
				case com.lzb.newloan.utils.Config.CODE_URL_ERROR:
					Toast.makeText(LoanDetailActivity.this, "url错误",
							Toast.LENGTH_SHORT).show();
					break;
				case com.lzb.newloan.utils.Config.CODE_NET_ERROR:
					Toast.makeText(LoanDetailActivity.this, "网络错误",
							Toast.LENGTH_SHORT).show();
					// if (time!=null) {
					// time.cancel();
					// }
					break;


				default:
					break;
			}
		};
	};

	private void initData() {
		com.lzb.newloan.utils.HttpUtils.doGetAsyn(com.lzb.newloan.utils.Config.BORROWPERSONDETAIL_CORD + "&jkid="
				+ jkid + "&userid=" + userId, mHandler, com.lzb.newloan.utils.Config.CODE_BORROWPERSONDETAIL);

	}

	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.backpress:
			finish();
			break;
		case R.id.look_protocal:
			Intent intents = new Intent(LoanDetailActivity.this,
					com.lzb.newloan.ProblemActivity.class);
			intents.putExtra("title", "借款协议");
			intents.putExtra("url", com.lzb.newloan.utils.Config.INVEST_JK+"?jkid="+jkid);
			startActivity(intents);
			break;
		case R.id.fx:
		Intent intent = new Intent(LoanDetailActivity.this,
				com.lzb.newloan.ProblemActivity.class);
		intent.putExtra("title", "风险提示书");
		intent.putExtra("url", com.lzb.newloan.utils.Config.FX);
		startActivity(intent);
		break;
		case R.id.look_video:
			 Intent intentv = new Intent(Intent.ACTION_VIEW);
             intentv.setDataAndType(Uri.parse(spdz), "video/mp4");
             startActivity(intentv);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub
		
	}

	

}
